package com.bw.module_home

import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bw.module_home.widget.RedPacketEnum
import com.bw.module_home.widget.RedPacketEvent
import com.bw.module_home.widget.RedPacketView
import com.bw.module_home.widget.RedPacketWidget
import org.greenrobot.eventbus.EventBus

class HomeActivity : AppCompatActivity() {
    private lateinit var redPacketWidget: RedPacketWidget
    private lateinit var rv: RecyclerView
    private lateinit var myAdapter: MyAdapter

    private var isStart = false
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_home)
        rv = findViewById(R.id.rv)
        myAdapter = MyAdapter()
        rv.adapter = myAdapter
        rv.layoutManager  = LinearLayoutManager(this)
        val mList = mutableListOf<String>()
        for (i in 0..1000){
            mList.add(i.toString())
        }
        myAdapter.setData(mList)


        redPacketWidget = findViewById(R.id.red_view)
        redPacketWidget.setNum(300)
        redPacketWidget.setStateCallBack(object : RedPacketWidget.StateCallBack{
            override fun completed() {
                Toast.makeText(this@HomeActivity,"红包已领取",Toast.LENGTH_SHORT).show()
            }
        })

        //滑动控制红包
        rv.addOnScrollListener(object : RecyclerView.OnScrollListener(){
            override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
                super.onScrollStateChanged(recyclerView, newState)
                when(newState){
                    RecyclerView.SCROLL_STATE_IDLE -> { //滑动停止
                        EventBus.getDefault().post(RedPacketEvent(RedPacketEnum.PAUSE))
                    }
                    RecyclerView.SCROLL_STATE_DRAGGING -> {
                        if (isStart){
                            EventBus.getDefault().post(RedPacketEvent(RedPacketEnum.RESUME))
                        }else{
                            EventBus.getDefault().post(RedPacketEvent(RedPacketEnum.INIT))
                            isStart = true
                        }
                    }
                }
            }
        })
    }


    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().post(RedPacketEvent(RedPacketEnum.STOP))

    }

    class MyAdapter:RecyclerView.Adapter<MyAdapter.MyViewHolder>(){
        private val list = mutableListOf<String>()
        fun setData(list:MutableList<String>){
            this.list.clear()
            this.list.addAll(list)
            notifyDataSetChanged()
        }


        inner class MyViewHolder(tv:TextView):RecyclerView.ViewHolder(tv)

        override fun onCreateViewHolder(p0: ViewGroup, p1: Int): MyViewHolder {
            return MyViewHolder(TextView(p0.context))
        }

        override fun getItemCount(): Int {
           return list.size
        }

        override fun onBindViewHolder(p0: MyViewHolder, p1: Int) {
           val tv:TextView = p0.itemView as TextView
            tv.text = list[p1]
        }
    }
}